from typing import Optional
from sqlalchemy.orm import Session

from ..crud.base import CRUDBase
from ..models.counselor import Counselor
from ..schemas.counselor import CounselorCreate, CounselorUpdate

class CRUDCounselor(CRUDBase[Counselor, CounselorCreate, CounselorUpdate]):
    def get_by_user_id(self, db: Session, *, user_id: int) -> Optional[Counselor]:
        """通过用户ID获取辅导员"""
        return db.query(Counselor).filter(Counselor.user_id == user_id).first()

    def create_with_user(
            self, db: Session, *, obj_in: CounselorCreate, user_id: int
    ) -> Counselor:
        """创建与用户关联的辅导员"""
        obj_in_data = obj_in.model_dump()
        db_obj = Counselor(**obj_in_data, user_id=user_id)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj

counselor_crud = CRUDCounselor(Counselor)